package com.test.platform.viewmodel

import XTSReportRepository
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.test.platform.model.XTSReport
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class XTSReportViewModel(
    private val repository: XTSReportRepository = XTSReportRepository()
) : ViewModel() {
    private val _reports = MutableStateFlow<List<XTSReport>>(emptyList())
    val reports: StateFlow<List<XTSReport>> = _reports.asStateFlow()

    private val _loading = MutableStateFlow(false)
    val loading: StateFlow<Boolean> = _loading.asStateFlow()

    private val _error = MutableStateFlow<String?>(null)
    val error: StateFlow<String?> = _error.asStateFlow()

    // 加载报告列表
    fun loadReports() {
        viewModelScope.launch(Dispatchers.IO) {
            _loading.value = true
            try {
                val result = repository.getXTSReports()
                withContext(Dispatchers.Main) {
                    _reports.value = result
                    _error.value = null
                }
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    _error.value = e.message ?: "加载失败"
                }
            } finally {
                withContext(Dispatchers.Main) {
                    _loading.value = false
                }
            }
        }
    }

    // 创建新报告
    fun createXTSReport(
        owner: String,
        name: String,
        iterationVersion: String? = null,
        apiVersion: String? = null
    ) {
        viewModelScope.launch(Dispatchers.IO) {
            _loading.value = true
            try {
                val report = XTSReport(
                    owner = owner,
                    name = name,
                    iterationVersion = iterationVersion,
                    apiVersion = apiVersion
                )
                val createdReport = repository.createXTSReport(report)  // 使用返回的报告
                withContext(Dispatchers.Main) {
                    _reports.value = _reports.value + createdReport  // 直接更新列表
                    _error.value = null
                }
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    _error.value = e.message ?: "创建失败"
                }
            } finally {
                withContext(Dispatchers.Main) {
                    _loading.value = false
                }
            }
        }
    }

    fun updateXTSReport(id: Int, report: XTSReport) {
        viewModelScope.launch(Dispatchers.IO) {
            _loading.value = true
            try {
                val updatedReport = repository.updateXTSReport(id, report)
                withContext(Dispatchers.Main) {
                    _reports.value = _reports.value.map { 
                        if (it.id == id) updatedReport else it 
                    }
                    _error.value = null
                }
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    _error.value = e.message ?: "更新失败"
                }
            } finally {
                withContext(Dispatchers.Main) {
                    _loading.value = false
                }
            }
        }
    }

    fun deleteXTSReport(id: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            _loading.value = true
            try {
                val success = repository.deleteXTSReport(id)
                if (success) {
                    withContext(Dispatchers.Main) {
                        _reports.value = _reports.value.filter { it.id != id }
                        _error.value = null
                    }
                }
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    _error.value = e.message ?: "删除失败"
                }
            } finally {
                withContext(Dispatchers.Main) {
                    _loading.value = false
                }
            }
        }
    }

    private val _currentReport = MutableStateFlow<XTSReport?>(null)
        val currentReport: StateFlow<XTSReport?> = _currentReport.asStateFlow()
    
        fun loadReportById(id: Int) {
            viewModelScope.launch(Dispatchers.IO) {
                _loading.value = true
                try {
                    val result = repository.getXTSReportById(id)
                    withContext(Dispatchers.Main) {
                        _currentReport.value = result
                        _error.value = null
                    }
                } catch (e: Exception) {
                    withContext(Dispatchers.Main) {
                        _error.value = e.message ?: "加载失败"
                    }
                } finally {
                    withContext(Dispatchers.Main) {
                        _loading.value = false
                    }
                }
            }
        }
}